package com.yanqu.client;

import com.yanqu.client.db.DbMgr;
import com.yanqu.client.player.Player;
import com.yanqu.road.entity.servercenter.DbInfo;
import com.yanqu.road.message.YanQuMessage;
import com.yanqu.road.pb.union.UnionProto;
import com.yanqu.road.server.protocol.GameProtocol;
import com.yanqu.road.utils.YanQuMessageUtils;
import io.vertx.core.Future;
import io.vertx.core.Vertx;

import java.sql.*;
import java.util.ArrayList;
import java.util.List;


public class CreateUnion {
    static int unionAmount = 0;

    public static void main(String[] args) throws SQLException {
        int takeCount = 100;//Integer.valueOf(args[1]);
        int titleIdNeed = 10;
        int serverId = 99900523;
        List<String> ipPort = DbMgr.getGameWebIpPort(serverId);
        if (null == ipPort) {
            return;
        }
        DbInfo dbInfo = DbMgr.getGameServerDbInfo(serverId);
        if (null == dbInfo) {
            return;
        }
        List<String> openIds = DbMgr.getOpenIdBySql(titleIdNeed, takeCount, dbInfo);
        if (null == openIds) {
            System.out.println("取不到需要的openId！！");
            return;
        }

        Vertx vertx = Vertx.vertx();
        for (String openId : openIds) {
            Player player = new Player(ipPort.get(0), Integer.valueOf(ipPort.get(1)), openId, vertx, serverId);
            Future<Object> gameLogin = player.gameLogin();
            gameLogin.onSuccess(o -> {
                YanQuMessage yanQuMessage = YanQuMessageUtils.buildMessage(GameProtocol.S_UNION_CREATE, player.getUserId(),
                        UnionProto.UnionCreateReqMsg.newBuilder().setUnionName("openId" + openId).setWechatCode("").setQqCode("").setOutNotice("批量创建商会-" + openId).setIsAllowRandomJoin(true).setInnerNotice("nothing").setUnionFlag(1));
                player.sendMessage(yanQuMessage);
                unionAmount++;
                System.out.println("----------批量创建商会----" + openId + "---累计" + unionAmount);
            });
        }

        Runtime.getRuntime().addShutdownHook(new Thread() {

            @Override
            public void run() {
                //todo 这里关闭连接
                System.out.println("收到关闭事件");
            }
        });
    }
}